

<!--
All rights Reserved, Designed By www.youyacao.com
@Description:分类管理
@author:成都市一颗优雅草科技有限公司
@version V4.2
注意：本前端源码遵循 MIT开源协议本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:422108995 23625059584
开源gitee地址：https://gitee.com/youyacao/songshu-video-page
 -->

<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 分类管理
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
        >批量删除</el-button>
        <!-- <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select>-->
        <!--<el-input v-model="query.name" placeholder="用户名/用户ID/手机号" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>-->
        <el-button
          style="float: right;margin-bottom:20px; "
          type="primary"
          icon="el-icon-lx-add"
          class="handle-del mr10"
          @click="addType"
        >添加</el-button>
      </div>

      <el-table
        :default-sort = "{prop: 'sort_id', order: 'ascending'}"
        :data="tableData"
        border
        class="table"
        row-key="id"
        ref="multipleTable"
        lazy
        :tree-props="{children: 'sub_type'}"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID"  width="120" align="center" sortable></el-table-column>
        <el-table-column prop="sort_id" label="排序"  width="120" align="center" sortable></el-table-column>
        <el-table-column prop="name" align="center" label="分类名称" sortable></el-table-column>
        <el-table-column label="分类图片" align="center">
          <template slot-scope="scope">
            <el-image class="table-td-thumb"  :preview-src-list="[$utils.formatUrl(scope.row.icon)]" :src="$utils.formatUrl(scope.row.icon)"></el-image>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.page"
          :page-size="query.pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
    <!-- 添加弹出框 -->
    <el-dialog title="添加分类" :visible.sync="addVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="上级分类">
          <el-select v-model="form.pid" placeholder="选择上级分类" class="handle-select mr10">
            <el-option key="顶级分类" label="顶级分类" value="1"></el-option>
            <el-option v-for="(item) in tableData" :key="item.name" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序ID">
          <el-input v-model="form.sort_id"></el-input>
        </el-form-item>
        <el-form-item label="图标">
          <el-image v-show="form.icon" class="type-icon"  :preview-src-list="[$utils.formatUrl(form.icon)]" :src="$utils.formatUrl(form.icon)"></el-image>
          <el-upload
            class="upload-demo"
            :limit="1"
            :show-file-list="false"
            :data="{type:'img'}"
            :action="$store.state.frontUrl+'/api/api/upload'"
            :file-list="fileList"
            list-type="picture"
            :on-success="uploadSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
          <el-input type="hidden" v-model="form.icon"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTypeSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="分类名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="上级分类">
          <el-select v-model="form.pid" placeholder="选择上级分类" class="handle-select mr10">
            <el-option key="顶级分类" label="顶级分类" value="1"></el-option>
            <el-option v-for="(item) in tableData" :key="item.name" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序ID">
          <el-input v-model="form.sort_id"></el-input>
        </el-form-item>
        <el-form-item label="图标" >
            <el-image  class="type-icon"  :preview-src-list="[$utils.formatUrl(form.icon)]" :src="$utils.formatUrl(form.icon)"></el-image>
          <el-upload
            class="upload-demo"
            :limit="1"
            :show-file-list="false"
            :data="{type:'img'}"
            :action="$store.state.frontUrl+'/api/api/upload'"
            :file-list="fileList"
            list-type="picture"
            :on-success="uploadSuccess"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
          <el-input type="hidden" v-model="form.icon"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
    export default {
        name: "basetable",
        data() {
            return {
                fileList:[],
                query: {
                    name: "",
                    page: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                addVisible:false,
                total: 0,
                form: {},
                idx: -1,
                id: -1
            };
        },
        created() {
            this.getData();
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData() {
                this.$api.type.list(this.query).then(res => {
                    this.tableData = res.data;
                    this.total = res.total || 0;
                });
            }, handleRemove(file, fileList) {
                console.log(file, fileList);
            },
            handlePreview(file) {
                console.log(file);
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, "page", 1);
                this.getData();
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm("确定要删除吗？", "提示", {
                    type: "warning"
                })
                        .then(() => {

                            var arr = [row.id];
                            this.$api.type.delete({ ids: arr }).then(res => {
                                if (res.code == 1) {
                                    this.$message.error("删除失败,"+res.msg);
                                    return;
                                }
                                this.getData();
                                this.$message.success("删除成功");
                            });
                        })
                        .catch(() => {});
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                const length = this.multipleSelection.length;
                var arr = [];
                this.delList = this.delList.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    arr.push(this.multipleSelection[i].id);
                }
                this.$confirm("确定要删除吗？", "提示", {
                    type: "warning"
                })
                        .then(() => {
                            this.$api.type.delete({ ids: arr }).then(res => {
                                if (res.code == 1) {
                                    this.$message.error("删除失败,"+res.msg);
                                    return;
                                }
                                this.getData();
                                this.$message.success("删除成功");
                            });
                        })
                        .catch(() => {});
                this.multipleSelection = [];
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                this.form = row;
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                this.$api.type.update(this.form).then((res)=>{
                    if(res.code==1){
                        this.$message.error(`修改失败，`+res.msg);
                        return;
                    }
                    this.editVisible = false;
                    this.$message.success(`修改成功`);
                });

            },
            // 分页导航
            handlePageChange(val) {
                this.$set(this.query, "page", val);
                this.getData();
            },
            addType(){
                this.form={};
                this.addVisible = true;
            },
            addTypeSubmit(){
                this.$api.type.add(this.form).then(res => {
                    if (res.code == 1) {
                        this.$message.error(res.msg)
                        return
                    }
                    this.addVisible = false
                    this.$message.success(`添加成功`)
                    this.getData()
                })
            },
            uploadSuccess(response, file, fileList){

                if(response.code===0){
                    this.form.icon=response.data.url;
                    this.fileList=[];
                }else{
                    this.$message.error("上传失败,"+response.msg);
                }

            }
        }
    };
</script>

<style scoped>
  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }
  .table {
    width: 100%;
    font-size: 14px;
  }
  .red {
    color: #ff0000;
  }
  .mr10 {
    margin-right: 10px;
  }
  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }
  .type-icon{
    display: block;
    width: 40px;
    height: 40px;
  }
</style>
